# Draw a grid of 1 inch squares, 0.1 inch tick marks, and label things.
#
###########################################################################

from g import *
from versiondec import VersionDec as vd

# Set the following variable to nonzero if you want to send the generated
# files directly to a PostScript printer.  Otherwise, set it to zero.

wrap_in_PJL = 0

def SetUp(file, orientation=portrait, units=inches):
    '''Convenience function to set up the drawing environment and return a
    file object to the output stream.
    '''
    ofp = open(file, "w")
    ginitialize(ofp, wrap_in_PJL)
    setOrientation(orientation, units)
    return ofp

def AddTickMarks(tick_interval, tick_length, skip=0, down=0):
    '''Add 10 tick marks with a wider fifth one.  This assumes that
    the origin is at the beginning point we need to mark and that the
    tick marks start along the positive x axis between 0 and 1 and go
    down in the y direction.
    '''
    if down:
        tick_length = -tick_length
    for ix in range(1, 10):
        if skip:
            skip = skip-1
            continue
        x = ix/10.
        move(x, 0)
        if ix == 5:
            rline(0, tick_length*1.5)
        else:
            rline(0, tick_length)


def Grid(file):
    '''Draw a grid of 1 inch squares.
    '''
    s = SetUp(file, portrait, inches)
    translate(.25, .5)      # Center the grid on the page (assumes a sheet
                            # of paper of 8.5" by 11" (letter size).
    text_size = 11./72      # Use an 11 point font
    textSize(text_size)
    textName(SansBold)
    lineColor(red)
    textColor(green)
    lineWidth(.01)
    box_width  = 8
    box_height = 10
    delta      = 0.02       # Offset text just a little from the lines
    tick_mark_length = .05
 
    # Draw the horizontal lines
    for ix in range(box_height+1):
        move(0, ix)
        rline(box_width, 0)
        move(tick_mark_length + 2*delta, ix + delta)
        text("%d" % ix)
        # Put in tick marks
        if ix < box_height:
            push()
            translate(0, ix)
            rotate(90)
            AddTickMarks(tick_mark_length, .05, down=1)
            pop()
 
    # Draw the vertical lines
    for ix in range(box_width+1):
        move(ix, 0)
        rline(0, box_height)
        # Draw the rotated characters.  We push() the current graphics state
        # so that the rotation won't affect the next grid line.
        push()
        translate(ix, 0)
        rotate(-90)
        move(delta, delta-text_size/2.5)
        if ix != 0:  # Don't do the zero, since it will overlap with the other
            text("%d" % ix)
        pop()
        # Put in tick marks
        if ix < box_width:
            push()
            translate(ix, 0)
            AddTickMarks(tick_mark_length, .05, 1*(ix==0))
            pop()
    # Label the page so we know the orientation; put it on a white box
    text_size = 2*text_size
    textSize(text_size)
    fillOn()
    x = 3.25
    y = 5
    delta = .25
    lineWidth(.04)
    lineColor(blue)
    fillColor(white)
    move(x-delta, y-delta)
    roundedRectangle(2.0, .5, .3)
    move(x-delta/3, y-text_size/2.5)
    textColor(green)
    text("8\"x10\" grid")
    s.close()

Grid(vd("out/grid.ps"))
